<script setup>
import { ref } from 'vue'
const userInfo = ref({
  username: '',
  password: '',
  avatar: ''
})
const imageUrl = ref('')

const handleAvatarSuccess = (response, uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
}

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/png' && rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 5) {
    ElMessage.error('Avatar picture size can not exceed 5MB!')
    return false
  }
  return true
}
</script>

<template>
  <div class="center">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户信息</span>
        </div>
      </template>
      <el-form :model="userInfo" label-width="120px" size="large">
        <el-form-item label="用户名">
          <el-input v-model="userInfo.username" class="ml-3 w-35"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userInfo.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"
              ><Plus
            /></el-icon> </el-upload
        ></el-form-item>
      </el-form>
      <template #footer>
        <el-row justify="center">
          <el-button type="primary" @click="updateUserInfo">更新</el-button>
        </el-row>
      </template>
    </el-card>
  </div>
</template>

<style scoped>
/* .center {
  justify-content: center;
  align-items: center;
  height: 100vh;
} */
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
